package upload;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.catalina.core.ApplicationPart;

/**
 * Servlet implementation class UpLoadServlet
 */
@WebServlet("/upload/UpLoadServlet")
@MultipartConfig(location="C:/Program Files/Java/workplace/Test")
public class UpLoadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpLoadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=GBK");
		PrintWriter out = response.getWriter();
		Part part = request.getPart("file");
		if(part.getContentType().contains("image")){
			ApplicationPart ap = (ApplicationPart)part;
			String fileName1 = ap.getSubmittedFileName();
			System.out.println("/upload/"+fileName1);
			part.write("/upload/"+fileName1);
			out.write("文件上传成功");
		}else{
			out.println("请选择文件");
		}
	}

}
